clear
clear matrix
set more off
set mem 5000m 
set matsize 500 
capture log close
log using /ssb/ovibos/a1/swp/kav/wk24/allreforms/regresjon_baseline.log, replace

/*Baseline estimates: +-3 months, linear RD with triangular weights*/

****Analysis

*****regressions

use /ssb/ovibos/a1/swp/kav/wk24/allreforms/data/cohorts_87_92_tempcost1.dta

*symmetric 3 month window. 

*87-reform: 1.05.1987. 
gen reform87=1 if fodtaar>=mdy(5,1,1987) & fodtaar<mdy(8,1,1987)
replace reform87=0 if fodtaar<mdy(5,1,1987)& fodtaar>=mdy(2,1,1987)
tab b_month if reform87==1
tab b_month if reform87==0

*88-reform: 1.juli
gen reform88=1 if fodtaar>=mdy(7,1,1988) & fodtaar<mdy(10,1,1988)
replace reform88=0 if fodtaar<mdy(7,1,1988) & fodtaar>=mdy(4,1,1988)

tab b_month if reform88==1
tab b_month if reform88==0

*89-reform: 1.april
gen reform89=1 if fodtaar>=mdy(4,1,1989) & fodtaar<mdy(7,1,1989)
replace reform89=0 if fodtaar<mdy(4,1,1989) & fodtaar>=mdy(1,1,1989)
tab b_month if reform89==1
tab b_month if reform89==0


*90-reform: 1.5.1990: 
gen reform90=1 if fodtaar>=mdy(5,1,1990) & fodtaar<mdy(8,1,1990)
replace reform90=0 if fodtaar< mdy(5,1,1990)  & fodtaar>=mdy(2,1,1990)

tab b_month if reform90==1
tab b_month if reform90==0

*91-reform: 1.7.1991: 
gen reform91=1 if  fodtaar>=mdy(7,1,1991) & fodtaar<mdy(10,1,1991)
replace reform91=0 if fodtaar<mdy(7,1,1991) & fodtaar>=mdy(4,1,1991)
tab b_month if reform91==1
tab b_month if reform91==0


*92-reform: 1.4.1992: 
gen reform92=1 if fodtaar>=mdy(4,1,1992) & fodtaar<mdy(7,1,1992)
replace reform92=0 if fodtaar< mdy(4,1,1992) & fodtaar>=mdy(1,1,1992)
tab b_month if reform92==1
tab b_month if reform92==0


*93 reform: 1.april
gen reform93=1 if fodtaar>=mdy(4,1,1993) & fodtaar<mdy(7,1,1993)
replace reform93=0 if fodtaar<mdy(4,1,1993) & fodtaar>=mdy(1,1,1993)





foreach t in 87 88 89 90 91 92  {
gen nreform`t'=1 if reform`t'==0
replace nreform`t'=0 if reform`t'==1
}

***generate linear slopes
sum fodtaar if fodtaar==mdy(5,1,1987)
local threshold87=r(mean)
display `threshold87'

sum fodtaar if fodtaar==mdy(7,1,1988)
local threshold88=r(mean)
display `threshold88'


sum fodtaar if fodtaar==mdy(4,1,1989)
local threshold89=r(mean)
display `threshold89'

sum fodtaar if fodtaar==mdy(5,1,1990)
local threshold90=r(mean)
display `threshold90'

sum fodtaar if fodtaar==mdy(7,1,1991)
local threshold91=r(mean)
display `threshold91'


sum fodtaar if fodtaar==mdy(4,1,1992)
local threshold92=r(mean)
display `threshold92'

sum fodtaar if fodtaar==mdy(4,1,1993)
local threshold93=r(mean)
display `threshold93'




*days
foreach t in 87 88 89 90 91 92 {

gen dslope`t'=fodtaar-`threshold`t'' if reform`t'!=. 
gen dslope`t'_1= dslope`t'* reform`t'
gen dslope`t'_2=dslope`t'*nreform`t' 


}

***triangular weights



gen triw87=1-abs((fodtaar-`threshold87')/92) if reform87!=.
replace triw87=1/92 if triw87==0 & reform87!=.

gen triw88=1-abs((fodtaar-`threshold88')/92) if reform88!=.
replace triw88=1/92 if triw88==0 & reform88!=.

gen triw89=1-abs((fodtaar-`threshold89')/92) if reform89!=.
replace triw89=1/92 if triw89==0 & reform89!=.



gen triw90=1-abs((fodtaar-`threshold90')/92) if reform90!=.
replace triw90=1/92 if triw90==0 & reform90!=.

gen triw91=1-abs((fodtaar-`threshold91')/92) if reform91!=.
replace triw91=1/92 if triw91==0 & reform91!=.


gen triw92=1-abs((fodtaar-`threshold92')/92) if reform92!=.
replace triw92=1/92 if triw92==0 & reform92!=.

gen triw93=1-abs((fodtaar-`threshold93')/92) if reform93!=.
replace triw93=1/92 if triw93==0 & reform93!=.



/***controls: We include pre-determined control variables for father's and mother's years of education, father's and mother's age and age squared 
at birth, parent's county of residence and marital status prior to the birth, and an indicator for the gender of the child.*/


gen mageb2=mageb*mageb
gen fageb2=fageb*fageb



*unpaid and total leave in days instead of months
gen mluptot_wk=mluptot*4*7

gen totleave_wk=totleave*4*7




* eligible var

gen eligible=.

forvalues t=1987/1992 {
replace eligible=eligible`t' if b_year==`t'
}


forvalues t=87/92 {
foreach c in eligible {

reg `c' reform`t' dslope`t'_1 dslope`t'_2 [pw=triw`t'] 
eststo `c'_`t'

estpost sum `c' if reform`t'!=.  
eststo sum_`c'_`t' 

}


}



*****Balance_test father, some college, mother somme college. married year before birth



*married year before, married 14 years after
gen marryb_14=1 if marcfyb==1 & marrchf14==1
replace marryb_14=0 if marcfyb==1 & marrchf14==0

gen divorce=1-marryb_14
label var divorce "divorce"

*not married year before, married 14 years after
gen not_marryb_14=1 if marcfyb==0 & marrchf14==1
replace not_marryb_14=0 if marcfyb==0 & marrchf14==0

gen mluptot_days=mluptot_wk*7
label var mluptot_wk "weeks of unpaid leave"
label var mluptot_days "days of unpaid leave"





*test balanced sample
forvalues t=87/92 {
foreach c in meduyb feduyb mageb fageb {

reg `c' reform`t' dslope`t'_1 dslope`t'_2 [pw=triw`t'] if eligible==1
eststo `c'_`t'

estpost sum `c' if reform`t'!=. & eligible==1
eststo sum_`c'_`t' 

}

}

forvalues t=87/92 {

esttab meduyb_`t' feduyb_`t'  mageb_`t'  fageb_`t' eligible_`t'  using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/balancing_reg_`t'.tex",  label keep(reform`t') se cells(b(star fmt(3)) se(par fmt(3))) nonumbers prehead("\centering\begin{tabular}{lcccccccccccc}\\") postfoot("\end{tabular}") varlabels(reform`t' "`t'") nomtitles starlevels (* 0.1 ** 0.05 *** 0.01) replace



 
esttab  sum_meduyb_`t' sum_feduyb_`t'  sum_mageb_`t'  sum_fageb_`t' sum_eligible_`t' using  "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/balancing_mean_`t'.tex", label cells("mean(fmt(2))")  nostar nonumbers prehead("\centering\begin{tabular}{lcccccccc}\\") postfoot("\end{tabular}")  replace

}






**Linear specification., +-3 months, with controls
*reform effects in year of birth

foreach c in mluptot_wk {
foreach t in 87 88 89 90 91 92 {


reg `c' reform`t' dslope`t'_1 dslope`t'_2 mageb mageb2 fageb fageb2 meduyb feduyb marcfyb i.countybb i.kjonn [pw=triw`t'] if eligible==1
eststo `c'_`t'

estpost sum `c' if  reform`t'!=. & eligible==1
eststo sum_`c'_`t'

}
}

forvalues t=87/92 {

esttab mluptot_wk_`t'    using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/mlup_reg_`t'.tex",  label keep(reform`t') se cells(b(star fmt(3)) se(par fmt(3))) nonumbers prehead("\centering\begin{tabular}{lcccccccccccccccc}\\") postfoot("\end{tabular}") varlabels(reform`t' "`t'") nomtitles starlevels (* 0.1 ** 0.05 *** 0.01) replace


esttab sum_mluptot_wk_`t'   using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/mlup_mean_`t'.tex", label  cells("mean(fmt(2))") nostar nonumbers prehead("\centering\begin{tabular}{lcccccccccccccc}\\") postfoot("\end{tabular}") nomtitles starlevels (* 0.1 ** 0.05 *** 0.01) replace


}

clear matrix

**child outcomes: High school drop-outs and standardized written exams results





*compare the coefficient to the mean


forvalues t=87/92 {

foreach c in dropout skraver {

reg `c' reform`t' dslope`t'_1 dslope`t'_2 mageb mageb2 fageb fageb2 meduyb feduyb marcfyb i.countybb i.kjonn [pw=triw`t'] if eligible==1
eststo `c'_`t'

estpost sum `c' if  reform`t'!=. & eligible==1
eststo sum_`c'_`t'


}

}



forvalues t=87/92 {

esttab dropout_`t'  skraver_`t'  using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/childoutcomes_reg_`t'.tex", label keep(reform`t') se cells(b(star fmt(3)) se(par fmt(3))) nonumbers prehead("\centering\begin{tabular}{lcccccccccccccc}\\") postfoot("\end{tabular}") varlabels(reform`t' "`t'") nomtitles starlevels (* 0.1 ** 0.05 *** 0.01) replace


esttab sum_dropout_`t'  sum_skraver_`t'  using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/childoutcomes_mean_`t'.tex", label cells("mean(fmt(2))") nostar nonumbers prehead("\centering\begin{tabular}{lcccccccccccccc}\\") postfoot("\end{tabular}") nomtitles starlevels (* 0.1 ** 0.05 *** 0.01) replace


}


clear matrix




**labour market attachment and income: mother



forvalues t=87/92 {

foreach c in memp2 memptot minctot femptot finctot {

reg `c' reform`t' dslope`t'_1 dslope`t'_2 mageb mageb2 fageb fageb2 meduyb feduyb marcfyb i.countybb i.kjonn [pw=triw`t'] if eligible==1
eststo `c'_`t'

estpost sum `c' if  reform`t'!=. & eligible==1
eststo sum_`c'_`t'


}

}

forvalues t=87/92 {

esttab memp2_`t'  memptot_`t' minc2_`t'  minctot_`t' femptot_`t' finctot_`t' using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/labour_reg_`t'.tex", label keep(reform`t') se cells(b(star fmt(3)) se(par fmt(3))) nonumbers prehead("\centering\begin{tabular}{lccccccccc}\\") postfoot("\end{tabular}") varlabels(reform`t' "`t'") nomtitles starlevels (* 0.1 ** 0.05 *** 0.01) replace


esttab sum_memp2_`t'  sum_memptot_`t'  sum_minc2_`t'  sum_minctot_`t' sum_femptot_`t' sum_finctot_`t' using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/labour_mean_`t'.tex", label  cells("mean(fmt(2))") nostar nonumbers prehead("\centering\begin{tabular}{lccccccccc}\\") postfoot("\end{tabular}") nomtitles starlevels (* 0.1 ** 0.05 *** 0.01) replace


}

clear matrix







**gender gap in labour market attachment and income
**family size and composition




forvalues t=87/92 {

foreach c in ggemptot gginctot nchild14 divorce not_marryb_14 {

reg `c' reform`t' dslope`t'_1 dslope`t'_2 mageb mageb2 fageb fageb2 meduyb feduyb marcfyb i.countybb i.kjonn [pw=triw`t'] if eligible==1
eststo `c'_`t'

estpost sum `c' if  reform`t'!=. & eligible==1
eststo sum_`c'_`t'

}
}

forvalues t=87/92 {

esttab ggemptot_`t' gginctot_`t' nchild14_`t'  not_marryb_14_`t' divorce_`t'   using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/fam_reg_`t'.tex", label keep(reform`t') se cells(b(star fmt(3)) se(par fmt(3))) nonumbers prehead("\centering\begin{tabular}{lccccccccccccc}\\") postfoot("\end{tabular}") varlabels(reform`t' "`t'") nomtitles starlevels (* 0.1 ** 0.05 *** 0.01) replace


esttab   sum_ggemptot_`t'   sum_gginctot_`t' sum_nchild14_`t'  sum_not_marryb_14_`t'  divorce_`t' using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/fam_mean_`t'.tex", label  cells("mean(fmt(2))") nostar nonumbers prehead("\centering\begin{tabular}{lccccccccccccccc}\\") postfoot("\end{tabular}") nomtitles starlevels (* 0.1 ** 0.05 *** 0.01) replace


}



rename annuity_skatt askatt 
rename annuity_benefits abenefits 
rename annuity_disp_faminc  adisp_faminc 

rename annuity_disp_faminc_ext2 adisp_faminc_ext2 




clear matrix

forvalues t=87/91 {

foreach c in cost_direct askatt abenefits adisp_faminc  adisp_faminc_ext2  {

reg `c' reform`t' dslope`t'_1 dslope`t'_2 mageb mageb2 fageb fageb2 meduyb feduyb marcfyb i.countybb i.kjonn [pw=triw`t'] if eligible==1
eststo `c'_`t'

estpost sum `c' if  reform`t'!=. & eligible==1
eststo s`c'_`t'

}
}


forvalues t=92/92 {

foreach c in cost_direct askatt abenefits adisp_faminc {

reg `c' reform`t' dslope`t'_1 dslope`t'_2 mageb mageb2 fageb fageb2 meduyb feduyb marcfyb i.countybb i.kjonn [pw=triw`t'] if eligible==1
eststo `c'_`t'

estpost sum `c' if  reform`t'!=. & eligible==1
eststo s`c'_`t'

}
}



forvalues t=87/91 {

esttab  cost_direct_`t' askatt_`t' abenefits_`t' adisp_faminc_`t' adisp_faminc_ext2_`t' using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/cost_benefit_`t'.tex", label keep(reform`t') se cells(b(star fmt(3)) se(par fmt(3))) nonumbers prehead("\centering\begin{tabular}{lccccccccccccc}\\") postfoot("\end{tabular}") varlabels(reform`t' "`t'")  starlevels (* 0.1 ** 0.05 *** 0.01) replace


esttab  scost_direct_`t' saskatt_`t' sabenefits_`t' sadisp_faminc_`t' sadisp_faminc_ext2_`t'  using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/cost_benefit_mean_`t'.tex", label  cells("mean(fmt(2))") nostar nonumbers prehead("\centering\begin{tabular}{lcccccccccccc}\\") postfoot("\end{tabular}") starlevels (* 0.1 ** 0.05 *** 0.01) replace


}

forvalues t=92/92 {

esttab  cost_direct_`t' askatt_`t' abenefits_`t' adisp_faminc_`t' using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/cost_benefit_`t'.tex", label keep(reform`t') se cells(b(star fmt(3)) se(par fmt(3))) nonumbers prehead("\centering\begin{tabular}{lccccccccccccc}\\") postfoot("\end{tabular}") varlabels(reform`t' "`t'")  starlevels (* 0.1 ** 0.05 *** 0.01) replace


esttab  scost_direct_`t' saskatt_`t' sabenefits_`t' sadisp_faminc_`t' using "/ssb/ovibos/a1/swp/kav/wk24/allreforms/table/cost_benefit_mean_`t'.tex", label  cells("mean(fmt(2))") nostar nonumbers prehead("\centering\begin{tabular}{lcccccccccccc}\\") postfoot("\end{tabular}") starlevels (* 0.1 ** 0.05 *** 0.01) replace


}




log close
